package com.woniuxy.yuekeuser.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.yuekeuser.common.Result;
import com.woniuxy.yuekeuser.dao.CampsiteMapper;
import com.woniuxy.yuekeuser.info.*;
import com.woniuxy.yuekeuser.model.*;
import com.woniuxy.yuekeuser.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

import java.util.List;
import java.util.UUID;

/**
 * 商家的controller
 */
@RestController
@Slf4j
@CrossOrigin("*")
public class BusinessController {
    @Autowired
    private IHotolcarService iHotolcarService;
    @Autowired
    private ICampsiteService iCampsiteService;
    @Autowired
    private IHotolcarImageService iHotolcarImageService;
    @Autowired
    private ICampsiteFoodService iCampsiteFoodService;
    @Autowired
    private ICampsiteActivityService iCampsiteActivityService;
    @Autowired
    private IOrderService IOrderService;
    @Autowired
    private IIncomeService iIncomeService;
    @Autowired
    private IUserService iUserService;



    /**
     * 接收单个文件上传，并返回路径
     *
     * @param file
     * @return
     */
    @PostMapping("addimage")
    public Result addimage (MultipartFile file){
        //图片存放路径
        String path = UUID.randomUUID()+"_"+file.getOriginalFilename();
        //判断是否为空
        if(!file.isEmpty()){
            try {
                file.transferTo(new File("D:/image/"+path));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        //返回给前端的图片访问路径
        return Result.success("http://localhost:8000/"+path);
    }

    /**
     * 添加酒店房车，包含基本信息和图
     */
    @PostMapping("addHotelCar")
    public Result addHotelCar(@RequestBody HotolcarInfo hotolcarInfo, HttpSession session){
        log.info("前端传过来的相关数据信息{}",hotolcarInfo);
        //获取图片集合
        List<FileItem> hotolcarimages = hotolcarInfo.getHotolcarimages();

        //创建hotocar对象
        Hotolcar hotolcar = new Hotolcar();
        hotolcar.setHotolcarName(hotolcarInfo.getHotolcarName());
        hotolcar.setHotolcarPeopleNumber(hotolcarInfo.getHotolcarPeopleNumber());
        hotolcar.setHotolcarPrice(hotolcarInfo.getHotolcarPrice());
        hotolcar.setHotolcarNumber(hotolcarInfo.getHotolcarNumber());
        hotolcar.setHotolcarWords(hotolcarInfo.getHotolcarWords());
        UserInfo user = (UserInfo) session.getAttribute("User");
        hotolcar.setUserId(user.getUserId());
        //根据userid查询营地id
        int i = iCampsiteService.findcampsiteIdByuserId(user.getUserId());
        hotolcar.setCampsiteId(i);
        //hotolcar表新增数据
        iHotolcarService.addHotelCar(hotolcar);
//        //根据酒店房车名查询酒店房车id
//        int HotoCarId = iHotolcarService.findHotoCarIdByHotoCarName(hotolcarInfo.getHotolcarName());
        //该酒店房车相关的图片添加
        for(FileItem fileItem : hotolcarimages){
            iHotolcarImageService.addhotolcarpath(hotolcar.getHotolcarId(),fileItem.getUrl());
        }

        return Result.success();

    }

    /**
     * 添加美食
     *
     */
    @PostMapping("addfood")
    public Result addfood(@RequestBody FoodInfo foodInfo, HttpSession session){
        log.info("前端传过来的信息{}",foodInfo);
        //获取登录的用户信息
        UserInfo user = (UserInfo) session.getAttribute("User");
        //根据userid查询营地id
        int i = iCampsiteService.findcampsiteIdByuserId(user.getUserId());
        //创建营地美食对象
        CampsiteFood campsiteFood = new CampsiteFood();
        campsiteFood.setCampsiteId(i);
        campsiteFood.setCampsiteFoodName(foodInfo.getCampsiteFoodName());
        campsiteFood.setCampsiteFoodWords(foodInfo.getCampsiteFoodWords());
        //营地美食表新增数据
        iCampsiteFoodService.addcampsiteFood(campsiteFood);
        //获取美食图片集合
        List<FileItem> foodimages = foodInfo.getFoodimages();
        for(FileItem fileItem : foodimages){
            //创建美食图片对象
            CampsiteFoodPicture campsiteFoodPicture = new CampsiteFoodPicture();
            campsiteFoodPicture.setCampsiteFoodId(campsiteFood.getCampsiteFoodId());
            campsiteFoodPicture.setCampsiteFoodPictureAddress(fileItem.getUrl());
            //美食图片表新增数据
            iCampsiteFoodService.addCampsiteFoodImage(campsiteFoodPicture);
        }

        return  Result.success();
    }
    /**
     * 添加营地活动
     */
    @PostMapping("addActivity")
    public Result addActivity(@RequestBody ActivityInfo activityInfo,HttpSession session){
        log.info("前端传过来的信息{}",activityInfo);
        //获取登录的用户信息
        UserInfo user = (UserInfo) session.getAttribute("User");
        //根据userid查询营地id
        int i = iCampsiteService.findcampsiteIdByuserId(user.getUserId());
        //创建营地活动对象
        CampsiteActivity campsiteActivity = new CampsiteActivity();
        campsiteActivity.setCampsiteId(i);
        campsiteActivity.setCampsiteActivityName(activityInfo.getCampsiteActivityName());
        campsiteActivity.setCampsiteActivityWords(activityInfo.getCampsiteActivityWords());
        iCampsiteActivityService.addCampsiteActivity(campsiteActivity);
        //获取营地活动的图片集合
        List<FileItem> activityimages = activityInfo.getActivityimages();
        for(FileItem fileItem : activityimages){
            //创建营地活动图片对象
            CampsiteActivityPicture campsiteActivityPicture = new CampsiteActivityPicture();
            campsiteActivityPicture.setCampsiteActivityId(campsiteActivity.getCampsiteActivityId());
            campsiteActivityPicture.setCampsiteActivityPictureAddress(fileItem.getUrl());
            //营地活动图片表新增数据
            iCampsiteActivityService.addcampsiteActivityPicture(campsiteActivityPicture);
        }

        return Result.success();

    }

    /**
     * 根据商家id分页查询该商家旗下的酒店房车
     */
    @GetMapping("findHotocars")
    public Result findHotocars(@RequestParam(defaultValue = "1") int pageIndex, @RequestParam(defaultValue = "5") int pageSize, HttpSession session){
        //获取登录的用户信息
        UserInfo user = (UserInfo) session.getAttribute("User");
        //根据商家id查询营地名字
        String campsiteName = iCampsiteService.findcampsiteNameByuserId(user.getUserId());
        int index = (pageIndex-1)*pageSize;
        //分页查询酒店房车
        List<Hotolcar> hotocars = iHotolcarService.findHotocars(campsiteName, index, pageSize);
        for(Hotolcar hotolcar : hotocars){
            hotolcar.setHotolcarImages(iHotolcarService.findcarImageByCarId(hotolcar.getHotolcarId()));
        }
        //获取总条数
        int count = iHotolcarService.findHotolcarCount(campsiteName);
        //获取总页码
        int countPage = -1;
        if(count<pageSize){
            countPage = 1 ;
        }else{
            countPage = (count%pageSize ==0 ? count/pageSize : (count/pageSize)+1);
        }
        PageInfo pageInfo = new PageInfo();
        pageInfo.setList(hotocars);
        pageInfo.setCount(count);
        pageInfo.setCountPage(countPage);
        pageInfo.setPageIndex(pageIndex);
        return  Result.success(pageInfo);
    }


    /**
     * 根据商家id分页查询该商家旗下营地的美食
     */
    @GetMapping("findFoods")
    public Result findFoods(@RequestParam(defaultValue = "1") int pageIndex, @RequestParam(defaultValue = "5") int pageSize,HttpSession session){
        //获取登录的用户信息
        UserInfo user = (UserInfo) session.getAttribute("User");
        //根据商家id查询营地名字
        String campsiteName = iCampsiteService.findcampsiteNameByuserId(user.getUserId());
        int index = (pageIndex-1)*pageSize;
        //分页查询营地美食
        List<CampsiteFood> foods = iCampsiteFoodService.findFoods(campsiteName, index, pageSize);
        for(CampsiteFood food : foods){
            food.setFoodImages(iCampsiteFoodService.findFoodImagesByFoodId(food.getCampsiteFoodId()));
        }
        //获取总条数
        int count = iCampsiteFoodService.findFoodCount(campsiteName);
        //获取总页码
        int countPage = -1;
        if(count<pageSize){
            countPage = 1 ;
        }else{
            countPage = (count%pageSize ==0 ? count/pageSize : (count/pageSize)+1);
        }
        PageInfo pageInfo = new PageInfo();
        pageInfo.setList(foods);
        pageInfo.setCount(count);
        pageInfo.setCountPage(countPage);
        pageInfo.setPageIndex(pageIndex);
        return  Result.success(pageInfo);
    }


    /**
     * 根据商家id分页查询该商家旗下营地的活动
     */
    @GetMapping("findActivitys")
    public Result findActivitys(@RequestParam(defaultValue = "1") int pageIndex, @RequestParam(defaultValue = "5") int pageSize, HttpSession session){
        //获取登录的用户信息
        UserInfo user = (UserInfo) session.getAttribute("User");
        //根据商家id查询营地名字
        String campsiteName = iCampsiteService.findcampsiteNameByuserId(user.getUserId());
        int index = (pageIndex-1)*pageSize;
        //分页查询营地活动
        List<CampsiteActivity> activitys = iCampsiteActivityService.findActivitys(campsiteName, index, pageSize);
        for(CampsiteActivity activity : activitys){
            activity.setActivityImages(iCampsiteActivityService.findActivityImagesByActivityId(activity.getCampsiteActivityId()));
        }
        //获取总条数
        int count = iCampsiteActivityService.findActivityCount(campsiteName);
        //获取总页码
        int countPage = -1;
        if(count<pageSize){
            countPage = 1 ;
        }else{
            countPage = (count%pageSize ==0 ? count/pageSize : (count/pageSize)+1);
        }
        PageInfo pageInfo = new PageInfo();
        pageInfo.setList(activitys);
        pageInfo.setCount(count);
        pageInfo.setCountPage(countPage);
        pageInfo.setPageIndex(pageIndex);
        return  Result.success(pageInfo);
    }
    /**
     * 根据商家id查询订单
     */
    @GetMapping("findBusinesOrder")
    public Result findBusinesOrder(@RequestParam(defaultValue = "1") int pageIndex, @RequestParam(defaultValue = "5") int pageSize,@RequestParam(defaultValue = "") String state, HttpSession session){
        //获取登录的用户信息
        UserInfo user = (UserInfo) session.getAttribute("User");
        //获取商家id
        int userId = user.getUserId();
//        int userId = 1;
        //计算分页参数
        int index = (pageIndex-1)*pageSize;
        List<Order> list = IOrderService.findBusinesOrder(userId, state, index, pageSize);
        System.out.println(list);
        //获取总条数
        int count = IOrderService.findBusinesOrderCount(userId,state);
        //获取总页码
        int countPage = -1;
        if(count<pageSize){
            countPage = 1 ;
        }else{
            countPage = (count%pageSize ==0 ? count/pageSize : (count/pageSize)+1);
        }
        PageInfo pageInfo = new PageInfo();
        pageInfo.setList(list);
        pageInfo.setCount(count);
        pageInfo.setCountPage(countPage);
        pageInfo.setPageIndex(pageIndex);
        return Result.success(pageInfo);
    }
    //查询商家收入
    @GetMapping("findBusinesIncome")
    public Result findBusinesIncome(@RequestParam(defaultValue = "1") int pageIndex, @RequestParam(defaultValue = "5") int pageSize, HttpSession session){
        //获取登录的用户信息
        UserInfo user = (UserInfo) session.getAttribute("User");
        //获取商家id
        int userId = user.getUserId();
//        int userId = 1;
        Page page = new Page();
        page.setCurrent(pageIndex);
        page.setSize(pageSize);
        IPage iPage = iIncomeService.getUserIncome(page, userId);
        System.out.println(iPage.getRecords());
        return Result.success(iPage);
    }

    //修改商家个人信息
    @PostMapping("updatamessige")
    public Result updatamessige(@RequestBody User user,HttpSession session){
        //获取登录的用户信息
        UserInfo loginuser = (UserInfo) session.getAttribute("User");
        //获取商家id
        int userId = loginuser.getUserId();
        user.setUserId(userId);
        log.info("前端传过来的数据{}",user);
        iUserService.updatamessige(user);
        return Result.success();
    }

    //获取商家个人信息
    @GetMapping("getBusinessMessige")
    public Result getBusinessMessige(HttpSession session){
        //获取登录的用户信息
        UserInfo user = (UserInfo) session.getAttribute("User");
        //获取商家id
        int userId = user.getUserId();
//        int userId = 1;
        User newuser = iUserService.findUserByUserId(userId);
        return Result.success(newuser);

    }

}
